package com.tongtong.stsuserapi.service.login.Impl;

import com.tongtong.stscommon.enumration.Message;
import com.tongtong.stscommon.enumration.Status;
import com.tongtong.stscommon.exception.STSAuthenticationException;
import com.tongtong.stscommon.message.STSMessage;
import com.tongtong.stscommon.message.STSSuccessMessage;
import com.tongtong.stscommon.utils.JwtUtil;
import com.tongtong.stscommon.utils.RedisCache;
import com.tongtong.stsuserapi.dao.UserDao;
import com.tongtong.stsuserapi.pojo.authentication.LoginManager;
import com.tongtong.stsuserapi.pojo.authentication.LoginUser;
import com.tongtong.stsuserapi.service.login.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 用户登录业务
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {

	@Autowired
	UserDao userDao;

	@Autowired
	private AuthenticationManager authenticationManager;

	@Autowired
	private RedisCache redisCache;

	@Override
	public STSMessage login(String username, String password) throws Exception{

		// AuthenticationManager authenticate进行用户认证
		UsernamePasswordAuthenticationToken authenticationToken =
				new UsernamePasswordAuthenticationToken(username,password);
		Authentication authenticate = authenticationManager.authenticate(authenticationToken);

		// 如果认证不通过，给出对应的提示
		if(Objects.isNull(authenticate)) {
			throw new STSAuthenticationException(Status.AuthenticationError,Message.AuthorizeFailed);
		}

		// 根据用户角色发放令牌
		if(authenticate.getPrincipal() instanceof LoginManager) {
			LoginManager loginManager = (LoginManager) authenticate.getPrincipal();
			String managerId = loginManager.getManager().getId().toString();
			String jwt = JwtUtil.createJWT("manager:"+managerId);
			Map<String,String> map = new HashMap<>();
			map.put("token",jwt);
			map.put("role","manager");

			redisCache.deleteObject("login:manager:"+managerId);
			redisCache.setCacheObject("login:manager:"+managerId,loginManager);
			return STSSuccessMessage.from(map);

		}
		else
		{
			LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
			String userid = loginUser.getUser().getId().toString();
			String jwt = JwtUtil.createJWT("user:"+userid);
			Map<String, String> map = new HashMap<>();
			map.put("token", jwt);
			map.put("role","user");

			redisCache.deleteObject("login:user:"+userid);
			redisCache.setCacheObject("login:user:" + userid, loginUser);
			return STSSuccessMessage.from(map);
		}
	}

	@Override
	public STSMessage logout() {

		// 获取SecurityContextHolder中的userid
		UsernamePasswordAuthenticationToken authentication =
				(UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();

		if(authentication.getPrincipal() instanceof LoginManager) {
			LoginManager loginManager = (LoginManager) authentication.getPrincipal();
			Long managerId = loginManager.getManager().getId();

			redisCache.deleteObject("login:manager:"+managerId);
		} else {
			LoginUser loginUser = (LoginUser) authentication.getPrincipal();
			Long userid = loginUser.getUser().getId();

			redisCache.deleteObject("login:user:"+userid);
		}

		return STSSuccessMessage.from(Status.OK, Message.OK);
	}

	@Override
	public STSMessage judgeLoginStatus() {
		return STSMessage.from(Status.OK,Message.OK);
	}

}
